from django.conf import settings
from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from question.models import Label
from user.models import User
from article.search_indexes import ArticleIndex
from .models import Channel, Article, Comment



class ArticleIndexSerializer(HaystackSerializer):
    """
    Article索引结果数据序列化器
    """
    class Meta:
        index_classes = [ArticleIndex]
        fields = ('text', 'id', 'title', 'content', 'createtime')


# 发布文章
class CreateArticlesSerializer(serializers.ModelSerializer):

    articleid = serializers.IntegerField(read_only=True, source='id')
    user_id = serializers.IntegerField(write_only=True)
    # channel = serializers.PrimaryKeyRelatedField(queryset=Channel.objects.all(), write_only=True, many=True)

    class Meta:
        model = Article
        fields = ['articleid', 'content', 'labels', 'title', 'channel', 'image', 'user_id']
        extra_kwargs = {
            'content': {
                'write_only': True
            },
            'title': {
                'write_only': True
            },
            'image': {
                'write_only': True,
                'allow_blank': True
            },

        }


# 1.1.1 作者发布过的文章的信息  -->  ArticlesFromUserSerializer
class ArticlesFromUserSerializer(serializers.ModelSerializer):
    """文章列表中 用户信息的 发布文章信息"""

    class Meta:
        model = Article
        fields = ['id', 'title']
        extra_kwargs = {
            'title': {
                'read_only': True
            },
        }


# 1.1 作者信息  -->  ArtilesUserSerializer
class AutherSimpleSerializer(serializers.ModelSerializer):
    """文章列表中 发布者信息"""
    articles = ArticlesFromUserSerializer(read_only=True, many=True)
    fans = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = ['id', 'username', 'avatar', 'articles', 'fans']
        extra_kwargs = {
            'username': {
                'read_only': True
            },
            'avatar': {
                'read_only': True
            },
        }

    def get_fans(self, obj):
        fans = User.objects.filter(idols=obj)
        return AutherSimpleSerializer(fans, many=True).data


# 1、文章列表  -->  ArticleListSerializer
class ArticleListSerializer(serializers.ModelSerializer):
    user = AutherSimpleSerializer(read_only=True)
    # collected_users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    # TODO : 检查、测试
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ['id', 'title', 'content', 'createtime', 'user',
                  'collected_users', 'collected', 'image']

        extra_kwargs = {
            'title': {
                'read_only': True
            },
            'content': {
                'read_only': True
            },
            'createtime': {
                'read_only': True
            },
            'collected_users': {
                'read_only': True
            },
            'image': {
                'read_only': True
            },
        }


# 频道展示
class ArticleChannelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = ['id', 'name']
        extra_kwargs = {
            "id": {
                "read_only": True
            },
            "name": {
                "read_only": True
            },
        }


# 文章评论的子评论信息
class SubsSerializer(serializers.ModelSerializer):
    parent = serializers.PrimaryKeyRelatedField(read_only=True)
    subs = serializers.StringRelatedField(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = '__all__'


# 2.1 文章的评论信息
class CommentsForArticleSerializer(serializers.ModelSerializer):
    user = AutherSimpleSerializer(read_only=True)
    user_id = serializers.IntegerField(write_only=True)
    subs = SubsSerializer(many=True, read_only=True)
    # parent = serializers.PrimaryKeyRelatedField(allow_null=True, read_only=True, required=False)

    class Meta:
        model = Comment
        fields = '__all__'


# 2、文章详情
class ArticlesDetailSerializer(serializers.ModelSerializer):
    user = AutherSimpleSerializer(read_only=True)
    comments = CommentsForArticleSerializer(many=True, read_only=True)

    # labels = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = '__all__'


# class CommentSerializer(serializers.ModelSerializer):
#
#     user_id =
#
#     class Meta:
#         model = Comment
#         fields = '__all__'